/*
 * File:   adc.c
 * Author: EDY
 *
 * Created on 2024��6��16��, ����10:33
 */

#include <xc.h>

#include "adc.h"
#include "portdefine.h"

/**
  Section: Macro Declarations
*/

#define ACQ_US_DELAY 5

void (*ADC_InterruptHandler)(void);

/**
  Section: ADC Module APIs
*/

void ADC_Initialize(void)
{
    // set the ADC to the options selected in the User Interface

    // ADFM right; ADNREF VSS; ADPREF VDD; ADCS FOSC/8;
    ADCON1 = 0x00;

    // ADACT no_auto_trigger;
    //    ADACT = 0x00;

    // ADRESL 0;
    ADRESL = 0x00;

    // ADRESH 0;
    ADRESH = 0x00;

    // ADGO stop; ADON enabled; CHS ANA0;
    ADCON0 = 0x01;
}

void ADC_SelectChannel(adc_channel_t channel)
{
    // select the A/D channel
    ADCON0bits.CHS = channel;
    // Turn on the ADC module
    ADCON0bits.ADON = 1;
}

void ADC_StartConversion(void)
{
    // Start the conversion
    ADCON0bits.GO = 1;
}

bool ADC_IsConversionDone(void)
{
    // Start the conversion
    return ((bool)(!ADCON0bits.GO));
}

adc_result_t ADC_GetConversionResult(void)
{
    // Conversion finished, return the result
    return ((adc_result_t)((ADRESH << 8) + ADRESL));
}

adc_result_t ADC_GetConversion(adc_channel_t channel)
{
    // select the A/D channel
    ADCON0bits.CHS = channel;

    // Turn on the ADC module
    // ADCON0bits.ADON = 1;

    // Acquisition time delay
    //__delay_us(ACQ_US_DELAY);
    __delay_us(100);

    // Start the conversion
    ADCON0bits.GO = 1;

    __delay_ms(1);

    // Wait for the conversion to finish
    while (ADCON0bits.GO)
    {
    }

    // Conversion finished, return the result
    return ((adc_result_t)((ADRESH << 8) + ADRESL));
}

void ADC_TemperatureAcquisitionDelay(void)
{
    __delay_us(200);
}

uint16_t VolToPress(uint16_t Vol)
{
    static uint16_t Press = 0;
    Press = Vol * 100;
    return Press;
}
/**
 End of File
*/